import sxtwl
from django.http import JsonResponse
from django.views.decorators.http import require_http_methods

# from .lib.bazi import Lleap
from .lib.datas import *
from .lib.shen import hp, trans
import logging

logger = logging.getLogger('log')

# Create your views here.
@require_http_methods(["GET"])
def getBazi(request):
    year = request.GET.get('year', '1984')
    month = request.GET.get('month', '4')
    day = request.GET.get('day', '3')
    time = request.GET.get('time', '0')
    sex = request.GET.get('sex', '0')
    logger.info(year+month+day+time+sex)
    lunar = sxtwl.Lunar();
    day = lunar.getDayBySolar(int(year), int(month), int(day))
    gz = lunar.getShiGz(day.Lday2.tg, int(time))
    Gans = collections.namedtuple("Gans", "year month day time")
    Zhis = collections.namedtuple("Zhis", "year month day time")
    gans = Gans(year=Gan[day.Lyear2.tg], month=Gan[day.Lmonth2.tg],
                day=Gan[day.Lday2.tg], time=Gan[gz.tg])
    zhis = Zhis(year=Zhi[day.Lyear2.dz], month=Zhi[day.Lmonth2.dz],
                day=Zhi[day.Lday2.dz], time=Zhi[gz.dz])
    me = gans.day
    zhus = [item for item in zip(gans, zhis)]
    scores = {"金": 0, "木": 0, "水": 0, "火": 0, "土": 0}
    gan_scores = {"甲": 0, "乙": 0, "丙": 0, "丁": 0, "戊": 0, "己": 0, "庚": 0, "辛": 0,
                  "壬": 0, "癸": 0}

    for item in gans:
        scores[gan5[item]] += 5
        gan_scores[item] += 5

    for item in list(zhis) + [zhis.month]:
        for gan in zhi5[item]:
            scores[gan5[gan]] += zhi5[item][gan]
            gan_scores[gan] += zhi5[item][gan]

    me_attrs_ = ten_deities[me].inverse
    Lleap = "闰" if day.Lleap else ""

    # print("农历:", end='')
    # print("\t{}年{}{}月{}日".format(day.Lyear0 + 1984, Lleap, ymc[day.Lmc], rmc[day.Ldi]))
    #
    # print("\t天干：{}{}{}{}".format(gans.year, gans.month, gans.day, gans.time))
    # print("\t地支：{}{}{}{}".format(zhis.year, zhis.month, zhis.day, zhis.time))
    # print("\t天干十神：{}{}{}{}".format(ten_deities[me][gans.year], ten_deities[me][gans.month], '日主',
    #                                ten_deities[me][gans.time]))
    cshishen = {}
    for seq, item in enumerate(zhis):
        out = ''
        multi = 2 if item == zhis.month and seq == 1 else 1
        cshishen[seq] = []
        for gan in zhi5[item]:
            cshishen[seq].append(gan)
    # print("\t地支藏干十神：{}{}{}{}".format(cshishen[0], cshishen[1], cshishen[2], cshishen[3]))
    strong = gan_scores[me_attrs_['比肩']] + gan_scores[me_attrs_['劫财']] \
             + gan_scores[me_attrs_['偏印']] + gan_scores[me_attrs_['正印']]
    # print("五行分数", scores, '\t\t八字强弱：', strong)

    r_sizhu = {
        "Y": {"tiangan": gans.year, "dizhi": zhis.year, "tag": ""},
        "M": {"tiangan": gans.month, "dizhi": zhis.month, "tag": ""},
        "D": {"tiangan": gans.day, "dizhi": zhis.day, "tag": ""},
        "H": {"tiangan": gans.time, "dizhi": zhis.time, "tag": ""},
    }
    r_tiangan_shishen = {
        "Y": {"shishen": ten_deities[me][gans.year], "tag": hp(ten_deities[me][gans.year])},
        "M": {"shishen": ten_deities[me][gans.month], "tag": hp(ten_deities[me][gans.month])},
        "D": {"shishen": '日主', "tag": hp('日主')},
        "H": {"shishen": ten_deities[me][gans.time], "tag": hp(ten_deities[me][gans.time])},
    }
    y = [{"shishen": ten_deities[me][k], 'name': k, "tag": hp(ten_deities[me][k])} for k in cshishen[0]]
    m = [{"shishen": ten_deities[me][k], 'name': k, "tag": hp(ten_deities[me][k])} for k in cshishen[1]]
    d = [{"shishen": ten_deities[me][k], 'name': k, "tag": hp(ten_deities[me][k])} for k in cshishen[2]]
    h = [{"shishen": ten_deities[me][k], 'name': k, "tag": hp(ten_deities[me][k])} for k in cshishen[3]]
    y.reverse()
    m.reverse()
    d.reverse()
    h.reverse()
    r_dizhi_shishen = {
        "Y": y,
        "M": m,
        "D": d,
        "H": h,
    }
    wuxing = {"wuxing": [scores['木'], scores['水'], scores['金'], scores['土'], scores['火']], 'score': strong}
    return JsonResponse(
        {'sizhu': r_sizhu, 'tiangan_shishen': r_tiangan_shishen, 'dizhi_shishen': r_dizhi_shishen, 'wuxing': wuxing,
         'nongli': "{}年{}{}月{}".format(trans(day.Lyear0 + 1984), Lleap, ymc[day.Lmc], rmc[day.Ldi])})
